import java.awt.Graphics
import java.awt.Image
import java.awt.image.BufferedImage
import javax.imageio.ImageIO

includeTargets << grailsScript("Init")
includeTargets << new File("scripts/RenderAssets.groovy")

target(cropPaths:"Crops path segments to the appropriate size"){
	//depends(renderAssets)
	try{
		println("Cropping path images...")
		def target = 'web-app/images';
		def targetDir = new File("${target}");
				
		targetDir.eachFileRecurse{ file ->
			if( !file.name.endsWith("png") ) return
			
			if( file.parent =~ /.*circle_*./ )
				cropPathImage(file);
		}
	}catch(Exception e){
		e.printStackTrace();
	}
}

def scale(BufferedImage bImage, int scaleX, int scaleY){
    def scaledImage = bImage.getScaledInstance(scaleX,scaleY,Image.SCALE_AREA_AVERAGING);
    BufferedImage temporaryImage = new BufferedImage(scaleX, scaleY, bImage.getType());
    Graphics g = temporaryImage.getGraphics()
    g.drawImage(scaledImage, 0, 0, null)
    g.dispose()
    return temporaryImage;
}

setDefaultTarget( cropPaths );

def loadImage(imageSrc){
	return ImageIO.read(imageSrc);
}

def cropPathImage(file){
	def conv = [
		'inner.east'       :[[name:'v-l', x:140, y:60]]
		,'outer.east'      :[[name:'v-r', x:140, y:60]]
		,'outer.south.east':[[name:'d3-rb', x:105, y:15]]
		,'inner.south.east':[[name:'d3-rt', x:105, y:15]]
		,'inner.south.west':[[name:'d3-lt', x:35, y:15]]
		,'outer.south.west':[[name:'d3-lb', x:35, y:15]]
		,'outer.west.north':[[name:'d1-rt', x:0, y:90]]
		,'inner.west.north':[[name:'d1-rb', x:0, y:90]]
		,'inner.east.south':[[name:'d1-lt', x:140, y:30]]
		,'outer.east.south':[[name:'d1-lb', x:140, y:30]]
		,'inner.west.south':[[name:'d2-lt', x:0, y:30]]
		,'outer.west.south':[[name:'d2-lb', x:0, y:30]]
		,'inner.east.north':[[name:'d2-rb', x:140, y:90]]
		,'outer.east.north':[[name:'d2-rt', x:140, y:90]]
		,'outer.north'     :[[name:'h-lt', x:70, y:120]]
		,'inner.north'     :[[name:'h-lb', x:70, y:120]]
		,'outer.south'     :[[name:'h-ub', x:70, y:0]]
		,'inner.south'     :[[name:'h-ut', x:70, y:0]]
		,'inner.horizontal':[[name:'h-ut', x:70, y:0], [name:'h-lb', x:70, y:120]]
		,'inner.vertical'  :[[name:'v-l', x:140, y:60],[name:'v-l2', x:0, y:60]]
		,'inner.diagonal'  :[[name:'d3-rt', x:105, y:15],[name:'d3-lt', x:35, y:15]]
		,'inner.diagonal2' :[[name:'d1-rb', x:0, y:90],[name:'d2-rb', x:140, y:90]]
		,'inner.diagonal3' :[[name:'d1-lt', x:140, y:30], [name:'d2-lt', x:0, y:30]]
		,'inner.diagonal4'  :[[name:'d4-rt', x:105, y:105],[name:'d4-lt', x:35, y:105]]
		,'outer.horizontal':[[name:'h-lt', x:70, y:120],[name:'h-ub', x:70, y:0]]
		,'outer.vertical'  :[[name:'v-r', x:140, y:60], [name:'v-r2', x:0, y:60]]
		,'outer.diagonal'  :[[name:'d3-rb', x:105, y:15],[name:'d3-lb', x:35, y:15]]
		,'outer.diagonal2' :[[name:'d1-rt', x:0, y:90],[name:'d2-rt', x:140, y:90]]
		,'outer.diagonal3' :[[name:'d1-lb', x:140, y:30],[name:'d2-lb', x:0, y:30]]
		,'outer.diagonal4'  :[[name:'d4-rb', x:105, y:105],[name:'d4-lb', x:35, y:105]]
		,'fill.horizontal':[[name:'h-lf', x:70, y:120],[name:'h-uf', x:70, y:0]]
		,'fill.vertical'  :[[name:'v-f', x:140, y:60],[name:'v-f2', x:0, y:60]]
		,'fill.diagonal'  :[[name:'d3-rf', x:105, y:15],[name:'d3-lf', x:35, y:15]]
		,'fill.diagonal2' :[[name:'d1-rf', x:0, y:90],[name:'d2-lf', x:140, y:90]]
		,'fill.diagonal3' :[[name:'d1-lf', x:140, y:30],[name:'d2-rf', x:0, y:30]]
		,'fill.diagonal4'  :[[name:'d4-rf', x:105, y:105],[name:'d4-lf', x:35, y:105]]
		]
	
	def segments = file.name.split("[.]", 2)
	if( segments.length < 2 || segments[1] == 'png' ) return;
	def image = loadImage(file)
	if(image && image.height == 900 && image.width == 760 ){
        image = scale(image, 190, 225);
	}
	if(image && image.height == 225 && image.width == 190 ){
		def name = segments[0]
		def location = segments[1][0..-5]
	
		def orientation = 130;
		
		def width = image.width
		def height = image.height
		
		def swidth = 50;
		def sheight = 95;
		
		BufferedImage bi = null 
		
		if( conv[location] ){
			def zz = conv[location].clone()
			def props = name.split('[,]')
			
			if( props.length > 1 ){
			   switch( props[1] ){
			      case 'h':
				 orientation = 0; //hanging
				 def alt = null;
				 if( location.endsWith( 'diagonal2' ) ){
				    alt = conv[location[0..-2] + '3'].clone()
				 }else if( location.endsWith( 'diagonal3' ) ){
				    alt = conv[location[0..-2] + '2'].clone()
				 }else if( location.endsWith( 'diagonal' ) ){
				    if( location.startsWith('inner') )
				       alt = conv['outer.diagonal'].clone()
				    else if( location.startsWith('outer') )
				       alt = conv['inner.diagonal'].clone()
				    println "\n\nDiagonal swap"
				 }
				 if( alt ){
				    if( !location.endsWith('diagonal') ){
				       alt[0].name = zz[1].name
				       alt[1].name = zz[0].name
				    }else{
				       def tmp = alt[0].name
				       alt[0].name = alt[1].name
				       alt[1].name = tmp
				    }
				    zz = alt;
				 }else if(zz.size() > 1 ){
				    def tmp = zz[0].name
				    zz[0].name = zz[1].name
				    zz[1].name = tmp
				 }
				 break;
			      case 't':
				 orientation = 130; //tall
				 break;
			      default:
				 orientation = 130; //tall
				 break;
			   }
			   name = props[0]
			   try{
			      swidth = Integer.parseInt(props[2])
			      sheight = Integer.parseInt(props[3])
			   }catch(NumberFormatException e){
			
			   }
			}
			for(def z in zz){
				println("Cropping: ${name} - ${location} to ${file.parent}/${name}_${z.name}.png\t[${orientation}]")
				def zy = orientation ? orientation - z.y : z.y
				bi = image.getSubimage(z.x, zy,swidth,sheight);
				FileOutputStream fos = new FileOutputStream(file.parent + File.separator + name + "_" + z.name + ".png");
				if( bi != null )
					ImageIO.write(bi, "png", fos)
				fos.close();
			}
		}
	}

}
